iT邦幫忙

2022 iThome 鐵人賽

DAY 21
0

You are running an online fashion boutique. Your big annual sale is coming up, so you need to take stock of your inventory to make sure you're ready.

A single item in the inventory is represented by a map, and the whole inventory is a list of such maps.

1. Sort items by price

Implement the sort_by_price/1 function. It should take the inventory and return it sorted by item price, ascending.

2. Find all items with missing prices

After sorting your inventory by price, you noticed that you must have made a mistake when you were taking stock and forgot to fill out prices for a few items.

Implement the with_missing_price/1 function. It should take the inventory and return a list of items that do not have prices.

3. Update item names

You noticed that some item names have a word that you don't like to use anymore. Now you need to update all the item names with that word.

Implement the update_names/3 function. It should take the inventory, the old word that you want to remove, and a new word that you want to use instead. It should return a list of items with updated names.

4. Increment the item's quantity

Some items were selling especially well, so you ordered more, in all sizes.

Implement the increase_quantity/2 function. It should take a single item and a number n, and return that item with the quantity for each size increased by n.

5. Calculate the item's total quantity

To know how much space you need in your storage, you need to know how many of each item you have in total.

Implement the total_quantity/1 function. It should take a single item and return how many pieces you have in total, in any size.


https://exercism.org/tracks/elixir/exercises/boutique-inventory

defmodule BoutiqueInventory do
  @moduledoc """
  practice Enum module
  """

  @spec sort_by_price(inventory :: [any()]) :: [any()]
  def sort_by_price(inventory), do: inventory |> Enum.sort_by(fn item -> item.price end, :asc)

  @spec with_missing_price(inventory :: [any()]) :: [any()]
  def with_missing_price(inventory), do: inventory |> Enum.filter(fn item -> !item.price end)

  @spec update_names(inventory :: [any()], old_word :: String.t(), new_word :: String.t()) :: [any()]
  def update_names(inventory, old_word, new_word) do
    inventory
    |> Enum.map(fn item ->
      if String.contains?(item.name, old_word),
        # do: Enum.into(%{name: String.replace(item.name, old_word, new_word)}, item),
        do: %{item | name: String.replace(item.name, old_word, new_word)},
        else: item
    end)
  end

  @spec increase_quantity(item :: map(), count: Integer) :: map()
  def increase_quantity(item, count) do
    %{
      quantity_by_size:
        item.quantity_by_size
        |> Enum.reduce(%{}, fn {key, value}, result ->
          %{key => value + count} |> Enum.into(result)
        end)
    }
    |> Enum.into(item)
  end

  @spec total_quantity(item :: map()) :: Integer
  def total_quantity(item) do
    item.quantity_by_size |> Enum.reduce(0, fn {_, value}, result -> result + value end)
  end
end

上一篇
二十. Access Behaviour
下一篇
二十二. Binaries
系列文
戀戀 Elixir30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言